package com.example.pda.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;

import com.example.pda.R;
import com.example.pda.common.CommonAdapter;
import com.example.pda.common.CommonViewHolder;
import com.example.pda.data.model.HwList;
import com.example.pda.data.model.StockList;
import com.example.pda.databinding.AdapterTextItemBinding;

public class StockListAdapter<T> extends CommonAdapter<T> {

    private T curItem;

    @Override
    protected int layoutId(int viewType) {
        return R.layout.adapter_text_item;
    }

    @Override
    protected int variableId() {
        return 0;
    }

    @Override
    protected void convert(CommonViewHolder<T> holder, T entity, int position) {
        super.convert(holder, entity, position);
        AdapterTextItemBinding binding = (AdapterTextItemBinding) holder.getBinding();
        boolean isCurItem = false;
        if (entity instanceof HwList) {
            String hwId = ((HwList) entity).getHwcode();
            isCurItem = curItem != null && !TextUtils.isEmpty(hwId) && hwId.equals(((HwList) curItem).getHwcode());
            binding.tvText.setText(((HwList) entity).getHwname());
        }
        if (entity instanceof StockList) {
            String stockid = ((StockList) entity).getStockcode();
            isCurItem = curItem != null && !TextUtils.isEmpty(stockid) && stockid.equals(((StockList) curItem).getStockcode());
            binding.tvText.setText(((StockList) entity).getStockname());
        }
        binding.checkView.setVisibility(isCurItem ? View.VISIBLE : View.GONE);
        binding.checkView.setColor(Color.parseColor("#121212"));
        binding.tvText.setTextColor(isCurItem ?
                Color.parseColor("#121212") : Color.parseColor("#444444"));
    }


    public T getCurItem() {
        return curItem;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setCurItem(T curItem) {
        this.curItem = curItem;
        notifyDataSetChanged();
    }
}
